/*
 * Contains utilities that the bash uses.
 * author: lml
 */
#ifndef __BASHUTILS_H__
#define __BASHUTILS_H__

#ifdef __cplusplus
extern "C" {
#endif

/*------------------------------------------
 * progress bar
 * note:non-thread-safe.If shrinking the window
 * after running will cause display confusion.
 *------------------------------------------*/
typedef struct {
    long long minimum;      // the minimum possible value of the range
    long long maximum;      // the maximum possible value of the range
    int       width;        // the paceholder number of the progress display
    char*     placeholder;  // the string of the placeholder
}progress_t;

/*
 * progress error code
 */
#define PERR_OK         (0) // on success
#define PERR_INV_PRG    (1) // invalid progress
#define PERR_INV_RANGE  (2) // invalid value range
#define PERR_INV_WIDTH  (3) // invalid width
#define PERR_INV_VALUE  (4) // invalid value
#define PERR_INV_PLACE  (5) // invalid placeholder
#define PERR_INV_OSTM   (6) // invalid output stream

/*
 * initializes the progress struct
 * @param p      a pointer to an progress struct
 * @param min    the minimum value of the progress range
 * @param max    the maximum value of the progress range
 * @return progress error code
 */
extern int progress_init(progress_t* p, long long min, long long max);

/*
 * show progress when the range value changes
 * @param p      a pointer to an progress struct
 * @param value  the changed value
 * @return progress error code
 */
extern int progress_show(progress_t* p, long long value);

/*
 * frees the memory space which have been allocated by the progress
 * @param p     a pointer to an progress struct
 */
extern void progress_destory(progress_t* p);


/*---------------------------------------------------
 * status bar
 * note: non-thread-safe. the struct will be registered SIGALRM signal,
 * and registered SIG_DFL after stop. If shrinking the window after
 * running will cause display confusion.
 *
 * status error code
 *---------------------------------------------------*/
#define SERR_OK         (0) // on success
#define SERR_INV_RATE   (1) // invalid rate
#define SERR_INV_WIDTH  (2) // invalid width
#define SERR_NOT_INIT   (3) // not initialized
#define SERR_RE_INIT    (4) // re-initialized
#define SERR_NOT_START  (5) // not start
#define SERR_STARTED    (6) // already started

/*
 * initializes the status
 * @param rate   the refresh rate in second
 * @return status error code
 */
extern int status_init(int rate);

/*
 * start the status
 * @return status error code
 */
extern int status_start();

/*
 * stop the status
 * @return status error code
 */
extern int status_stop();

/*
 * destory the status
 */
extern int status_destory();

/*---------------------------------------------
 * display functions, display text with special style
 * while the output stream is linked to terminal
 *---------------------------------------------*/

/*
 * repeatedly overlap the text on the last single line
 */
extern void echo_overlap(const char* s);

/*
 * display a line text in normal style
 */
extern void echo_info(const char* s);

/*
 * display a line text in green color
 */
extern void echo_sucess(const char* s);

/*
 * display a line text in yellow color
 */
extern void echo_warn(const char* s);

/*
 * display a line text in red color
 */
extern void echo_error(const char* s);

/*
 * display a line text with underline
 */
extern void echo_underline(const char* s);

/*
 * display a line text with reversed color
 */
extern void echo_reverse(const char* s);

#ifdef __cplusplus
}
#endif

#endif // __BASHUTILS_H__

